package com.xiaoniu.imclient.networkModule.rong;

import android.text.TextUtils;

import com.xiaoniu.lib_keleim.ConversationType;
import com.xiaoniu.lib_keleim.Schemes;
import com.xiaoniu.lib_keleim.db.ConversationListCallBack;
import com.xiaoniu.lib_keleim.db.MessageCountCallBack;
import com.xiaoniu.lib_keleim.db.MessageListCallBack;
import com.xiaoniu.lib_keleim.pojo.vos.BaseMessage;
import com.xiaoniu.lib_keleim.pojo.vos.PreSendMessage;
import com.xiaoniu.lib_keleim.pojo.vos.UserInfoVO;
import com.xiaoniu.lib_keleim.pojo.vos.WrapperConversation;
import com.xiaoniu.lib_keleim.spi.INetworkModule;
import com.xiaoniu.lib_keleim.spi.INetworkModuleFactory;
import com.xiaoniu.lib_keleim.spi.Interceptor;
import com.xiaoniu.lib_keleim.spi.ReceiveMessageListener;
import com.xiaoniu.lib_keleim.utils.InterceptType;

import java.util.HashMap;
import java.util.List;

import io.rong.imlib.IRongCallback;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.MessageContent;

public class RongNetworkModuleFactory implements INetworkModuleFactory {


    @Override
    public Schemes getSupportedUriSchemes() {
        return Schemes.RONGYUN;
    }

    @Override
    public INetworkModule createNetworkModule() {
        return new RongNetworkModule();
    }

    public static class RongNetworkModule implements INetworkModule {
        private static final RongIMClient mClient = RongIMClient.getInstance();

        @Override
        public void connect(UserInfoVO userInfo) {
            String token = "token";
            RongIMClient.connect(token, new RongIMClient.ConnectCallback() {
                @Override
                public void onSuccess(String t) {

                }

                @Override
                public void onError(RongIMClient.ConnectionErrorCode e) {

                }

                @Override
                public void onDatabaseOpened(RongIMClient.DatabaseOpenStatus code) {

                }
            });
        }

        @Override
        public void disconnect() {
            mClient.logout();
            mClient.disconnect();
        }

        @Override
        public void joinRoom(String targetId) {
            mClient.joinChatRoom(targetId, -1, new RongIMClient.OperationCallback() {
                @Override
                public void onSuccess() {
                }

                @Override
                public void onError(RongIMClient.ErrorCode errorCode) {
                }
            });
        }

        @Override
        public void quitRoom(String targetId) {
            mClient.quitChatRoom(targetId,  new RongIMClient.OperationCallback() {
                @Override
                public void onSuccess() {
                }

                @Override
                public void onError(RongIMClient.ErrorCode errorCode) {
                }
            });
        }

        @Override
        public void joinUltraGroup(String targetId, String channelId) {

        }

        @Override
        public void quitUltraGroup(String targetId, String channelId) {

        }

        @Override
        public void insertMessage(BaseMessage message) {

        }

        @Override
        public void sendMessage(PreSendMessage message) {
            MessageContent messageContent = MessageTextBean.obtain(message.rawMessage);
            Message msg = Message.obtain(String.valueOf(message.targetId), Conversation.ConversationType.CHATROOM, messageContent);
            mClient.sendMessage(msg, null, null, new IRongCallback.ISendMessageCallback() {
                @Override
                public void onAttached(Message message) {
                }

                @Override
                public void onSuccess(Message message) {
                }

                @Override
                public void onError(Message message, RongIMClient.ErrorCode errorCode) {

                }
            });
        }

        @Override
        public void getMessages(ConversationType conversationType, String targetId, String channelId, MessageListCallBack callBack) {

        }

        @Override
        public void getConversations(ConversationType conversationType, String targetId, ConversationListCallBack callBack) {
            getConversations(conversationType, targetId,"", callBack);
        }

        @Override
        public void getConversations(ConversationType conversationType, String targetId, String channelId, ConversationListCallBack callBack) {
            Conversation.ConversationType rongConversationType = Conversation.ConversationType.setValue(conversationType.getValue());
            mClient.getConversationList(new RongIMClient.ResultCallback<List<Conversation>>() {
                @Override
                public void onSuccess(List<Conversation> conversations) {
                    WrapperConversation wrapperConversation = new WrapperConversation();
                    wrapperConversation.schemes = Schemes.RONGYUN;
                    wrapperConversation.fromLocal = false;
                    wrapperConversation.otherConversations = conversations;
                    callBack.onResult(wrapperConversation);
                }
                @Override
                public void onError(RongIMClient.ErrorCode e) {

                }
            }, rongConversationType);
        }

        @Override
        public void getUnreadCount(ConversationType conversationType, String targetId, String channelId, MessageCountCallBack callBack) {
            RongIMClient.ResultCallback<Integer> callback = new RongIMClient.ResultCallback<Integer>() {
                @Override
                public void onSuccess(Integer integer) {
                    if (callBack!=null) {
                        callBack.onResult(integer);
                    }
                }

                @Override
                public void onError(RongIMClient.ErrorCode e) {
                    if (callBack!=null) {
                        callBack.onResult(-1);
                    }
                }
            };
            Conversation.ConversationType rongConversationType = Conversation.ConversationType.setValue(conversationType.getValue());
            if (TextUtils.isEmpty(targetId)) {
                mClient.getUnreadCount(callback, rongConversationType);
            }else{
                mClient.getUnreadCount( rongConversationType,targetId,callback);
            }
        }

        @Override
        public void addOnReceiveMessageListener(ReceiveMessageListener receiveMessageListener) {

        }
    }
}
